package controller;

import jakarta.servlet.http.HttpServletRequest;
import jakarta.servlet.http.HttpSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.dao.DataAccessException;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import entity.SysUser;
import service.SysUserService;
import utils.Constants;

import java.util.HashMap;
import java.util.Map;

@Controller
public class LoginController extends BaseController {
    @Autowired
    private SysUserService sysUserService;

    @GetMapping(value = "/login")
    public String toLogin() {
        return "forward:/login.jsp";
    }

    @PostMapping(value = "/login")
    public String login(@RequestParam("account") String account,
                        @RequestParam("password") String password,
                        HttpServletRequest request,
                        HttpSession session) {
        //调用service方法，进行用户匹配
        SysUser loginUser = sysUserService.login(account,password);
        if(loginUser!=null){//登录成功
            // 登录成功，用户信息存入 session
            //session.setAttribute("loginUser", loginUser);
            session(Constants.SESSION_USER, loginUser);
            // 重定向到后台首页
            return "redirect:/main/index";
        }else{
            // 设置错误信息到 request 作用域
            request.setAttribute("error", "用户名或密码错误！ ");
            //request(Constants.REQUEST_ERROR,"用户名或密码错误！");
            throw new RuntimeException("登录失败，用户名或密码错误！ ");
            // 转发到登录页面
//            return "forward:/login.jsp";
        }
    }

    /**
     * 退出登录
     * @return
     */
    @GetMapping("/logout")
    public String logout(HttpServletRequest request,
                         HttpSession session){
        session.invalidate();
        return "redirect:/login";
    }
    // 跳转到后台首页
    @GetMapping("/main/index")
    public String index(){
        return "index";
    }

    @ExceptionHandler(value = RuntimeException.class)
    public String handlerException(RuntimeException e, HttpServletRequest request)
    {
        // 设置捕获的错误对象到 request 作用域
        request.setAttribute(Constants.REQUEST_ERROR, e);
        // 跳转异常页面： /WEB-INF/jsp/500.jsp
        return "500";
    }

    @ExceptionHandler(DataAccessException.class)
    public String handleDataAccessException(DataAccessException e, HttpServletRequest request)
    {
        // 设置捕获的错误对象到 request 作用域
        request.setAttribute(Constants.REQUEST_ERROR, e);
        // 跳转异常页面： /WEB-INF/jsp/nodb.jsp
        return "nodb";
    }

    @GetMapping("/main/changePwd")
    public String changePwd(){
        return "sys/changePwd";
    }

    @PostMapping("/main/changePwd")
    public String changePwd(@RequestParam("oldPassword")String oldPassword,@RequestParam("password") String password,HttpSession session){
        SysUser sysUser = (SysUser)session.getAttribute(Constants.SESSION_USER);
        sysUser.setPassword(password);
        sysUserService.update(sysUser);
        return "redirect:/login";
    }

    @GetMapping("/main/oldPasswordCheck")
    @ResponseBody
    public Object oldPasswordCheck(@RequestParam("oldPassword")String oldPassword,HttpSession session){
        Map<String, Object> map = new HashMap<>();
        SysUser sysUser = (SysUser)session.getAttribute(Constants.SESSION_USER);
        if(sysUser.getPassword().equals(oldPassword)){
            map.put("pass",1);
        }else {
            map.put("pass",0);
        }
        return map;
    }
}
